package com.lc.sell.controller;


import com.lc.sell.entity.Product;
import com.lc.sell.service.ProductService;
import com.lc.sell.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 龙晨
 * @since 2022-02-01
 */
@Api(tags = "商品控制器")
@RestController
@RequestMapping("product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping
    public ModelAndView toProductList(Integer id){
        ModelAndView model = new ModelAndView();
        model.setViewName("page/product_list");
        model.addObject("mid",id);
        return model;
    }

    @GetMapping("toProductEdit")
    public ModelAndView toProductEdit(@RequestParam(required = false) Integer id,Integer mid){
        ModelAndView model = new ModelAndView();
        model.setViewName("page/product_edit");
        model.addObject("mid",mid);
        model.addObject("product",productService.getById(id));
        return model;
    }

    @ApiOperation(value = "根据商家id查询商品")
    @GetMapping("findAllProductByMid")
    public ResultVo findAllProductByMid(
        Integer mid,
        @RequestParam(defaultValue = "1",name = "page") Long current,
        @RequestParam(defaultValue = "10") Long size){
       return productService.findAllProductByMid(mid, current, size);
    }

    @ApiOperation(value = "添加商品信息")
    @PostMapping("insertProduct")
    public ResultVo insertProduct(Product product){
        return productService.insertProduct(product);
    }

    @ApiOperation(value = "删除商品信息")
    @DeleteMapping("deleteProduct")
    public ResultVo deleteProduct(Integer pid){
        return productService.deleteProduct(pid);
    }

    @ApiOperation(value = "修改商品信息")
    @PostMapping("updateProduct")
    public ResultVo updateProduct(Product product){
        return productService.updateProduct(product);
    }

    @ApiOperation(value = "按条件查询商品信息")
    @GetMapping("findByProduct")
    public ResultVo findByProduct(Product product){
        return productService.findByProduct(product);
    }

    @ApiOperation(value = "修改商品上/下架")
    @PutMapping("updateProductIsSell")
    public ResultVo updateProductIsSell(Integer id){
        return productService.updateProductIsSell(id);
    }
}
